package com.dhcc.sds.standard.controller;


import java.util.List;
import java.util.Map;


import com.dhcc.core.framework.result.node.ZTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.service.ISdsStandSourceService;


/**
 * 数据源定义
 * 
 * @ClassName: SdsStandSourceController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:52
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstandsource")
public class SdsStandSourceController extends BaseController {

	private String PREFIX = "/backend/sds/sdsstandsource/";

	@Autowired
	private ISdsStandSourceService sdsStandSourceService;
	

    /**
     * 跳转到数据源定义列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsstandsource.html";
    }
	
    /**
     * 跳转到新增数据源定义
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsstandsource_add.html";
    }
	
	/**
     * 跳转到修改数据源定义
     */
    @RequestMapping("/to_update/{sdsStandSourceId}")
    public String toUpdate(@PathVariable Long sdsStandSourceId, Model model) {
        SdsStandSourceEntity sdsStandSource = this.sdsStandSourceService.selectById(sdsStandSourceId);
        model.addAttribute("sdsStandSource",sdsStandSource);
        return PREFIX + "sdsstandsource_edit.html";
    }

    /**
     * 获取数据源定义列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsStandSourceEntity> page = new PageFactory<SdsStandSourceEntity>().defaultPage();
        List<SdsStandSourceEntity> result = this.sdsStandSourceService.page(page,params);
        page.setRecords((List<SdsStandSourceEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增数据源定义
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsStandSourceEntity sdsStandSource) {
        if (CommonUtil.isOneEmpty(sdsStandSource)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsStandSource.insert();
        return R.ok();
    }

    /**
     * 删除数据源定义
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsStandSourceId) {
	
        this.sdsStandSourceService.deleteById(sdsStandSourceId);

        return R.ok();
    }

    /**
     * 修改数据源定义
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsStandSourceEntity sdsStandSource) {
        if (CommonUtil.isOneEmpty(sdsStandSource)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsStandSourceEntity old = this.sdsStandSourceService.selectById(sdsStandSource.getId());

        old.setSourceCode(sdsStandSource.getSourceCode());
        old.setSourceDesc(sdsStandSource.getSourceDesc());
        old.setIsActive(sdsStandSource.getIsActive());
        old.setResume(sdsStandSource.getResume());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 获取数据源的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> entityTree() {
        return sdsStandSourceService.getEntityTree();
    }
	
}
